package java8.chapter06.test;

import static java.util.stream.Collectors.reducing;
import static java8.chapter06.test.Dish.menu;

public class Reducing {

	public static void main(String[] args) {
		System.out.println("Total calories in menu: " + calculateTotalCalories());
		System.out.println("Total calories in menu: " + calculateTotalCaloriesWithMethodReference());
		System.out.println("Total calories in menu: " + calculateTotalCaloriesWithoutCollectors());
		System.out.println("Total calories in menu: " + calculateTotalCaloriesUsingSum());
	}

	private static int calculateTotalCaloriesUsingSum() {
		return menu.stream().mapToInt(Dish::getCalories).sum();
	}

	private static int calculateTotalCaloriesWithoutCollectors() {
		return menu.stream().map(Dish::getCalories).reduce(Integer::sum).get();
	}

	private static int calculateTotalCaloriesWithMethodReference() {
		return menu.stream().collect(reducing(0, Dish::getCalories, Integer::sum));
	}

	private static int calculateTotalCalories() {
		return menu.stream().collect(reducing(0, Dish::getCalories, (Integer i, Integer j) -> i + j));
	}

}